package com.ruoyi.lvlian.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.lvlian.domain.PaymentInfo;
import com.ruoyi.lvlian.domain.VehicleInfo;
import com.ruoyi.lvlian.dto.PaymentInfoDto;
import com.ruoyi.lvlian.mapper.PaymentInfoMapper;
import com.ruoyi.lvlian.service.IPaymentInfoService;
import com.ruoyi.lvlian.util.ExcelUtil;
import com.ruoyi.lvlian.vo.PaymentInfoDashboardVo;
import com.ruoyi.lvlian.vo.PaymentInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 支付信息Service业务层处理
 *
 * @author liyuxi
 * @date 2024-05-13
 */
@Service
public class PaymentInfoServiceImpl implements IPaymentInfoService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    /**
     * 查询支付信息
     *
     * @param id 支付信息主键
     * @return 支付信息
     */
    @Override
    public PaymentInfo selectPaymentInfoById(String id) {
        return paymentInfoMapper.selectPaymentInfoById(id);
    }

    /**
     * 查询发票对应的支付信息列表
     *
     * @param paymentInfo 支付信息
     * @return 支付信息
     */
    @Override
    public List<PaymentInfoVo> selectPaymentInfoList(PaymentInfoDto paymentInfo) {

        if (StringUtils.isNotEmpty(paymentInfo.getInvoiceNum())) {
            return paymentInfoMapper.queryPaymentInfoListByInvoiceNum(paymentInfo);
        }


        return paymentInfoMapper.selectPaymentInfoList(paymentInfo);
    }

    /**
     * 新增支付信息
     *
     * @param paymentInfo 支付信息
     * @return 结果
     */
    @Override
    public int insertPaymentInfo(PaymentInfo paymentInfo) {
        paymentInfo.setId("ZF" + MD5Utils.md5(paymentInfo.getPaymentNum()));
        paymentInfo.setCreateUser(SecurityUtils.getUsername());
        paymentInfo.setCreateDate(DateUtils.getCurrentTime());
        return paymentInfoMapper.insertPaymentInfo(paymentInfo);
    }

    /**
     * 修改支付信息
     *
     * @param paymentInfo 支付信息
     * @return 结果
     */
    @Override
    public int updatePaymentInfo(PaymentInfo paymentInfo) {
        int updateResult = paymentInfoMapper.updatePaymentInfo(paymentInfo);
        if (0 == updateResult) {
            updateResult = this.insertPaymentInfo(paymentInfo);
        }

        return updateResult;

    }

    /**
     * 批量删除支付信息
     *
     * @param ids 需要删除的支付信息主键
     * @return 结果
     */
    @Override
    public int deletePaymentInfoByIds(String[] ids) {
        return paymentInfoMapper.deletePaymentInfoByIds(ids);
    }

    /**
     * 删除支付信息信息
     *
     * @param id 支付信息主键
     * @return 结果
     */
    @Override
    public int deletePaymentInfoById(String id) {
        return paymentInfoMapper.deletePaymentInfoById(id);
    }

    @Override
    public List<PaymentInfoVo> queryPaymentInfoInvioceList(PaymentInfoDto paymentInfo) {

//        更新最新的剩余汇总金额
        paymentInfoMapper.updateInvoiceInfoPaymentInfoRefTotalUseAmount();
        // 查询该发票对应的支付单列表
        List<PaymentInfoVo> paymentInfoVos = paymentInfoMapper.queryPaymentInfoInvioceList(paymentInfo);
        // 查询发票关联支付的状态

        return paymentInfoVos;
    }

    @Override
    public List<PaymentInfo> importInfo(MultipartFile file, String sheetName, int rowNum) throws Exception {
        byte[] fileBytes = file.getBytes();
        List<PaymentInfo> invoiceRecyclingInfos = ExcelUtil.readExcel(fileBytes, PaymentInfo.class, sheetName, rowNum);
        return invoiceRecyclingInfos;
    }

    @Override
    public int bachSave(List<PaymentInfo> dtoList) {
        int saveBatch = 0;
        if (!dtoList.isEmpty()) {
            for (PaymentInfo po : dtoList) {
                if (po.getPaymentNum() == null || po.getPaymentNum().isEmpty()) {
                    continue; // 如果 paymentNum 为空，则跳过当前循环
                }

                po.setId(MD5Utils.md5(po.getPaymentNum()));
                po.setCreateUser(SecurityUtils.getUsername());
                po.setCreateDate(DateUtils.getCurrentTime());
            }

            String[] arr = dtoList.stream()
                    .map(PaymentInfo::getPaymentNum) // 提取出 invoiceNum
                    .distinct()                        // 去重
                    .toArray(String[]::new);

//             导入之前 先删除
            paymentInfoMapper.batchDelete(arr);

            saveBatch = paymentInfoMapper.batchSave(dtoList);
        }
        return saveBatch;
    }

    @Override
    public PaymentInfoDashboardVo queryPaymentInfoAgg(String category, String invoiceNum) {
//        TODO
        return paymentInfoMapper.queryPaymentInfoAgg(category  ,invoiceNum );
    }

}
